package cn.iocoder.yudao.module.hs.service.warehouse;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.hs.controller.admin.warehouse.vo.WarehousePageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.warehouse.vo.WarehouseSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.module.hs.dal.mysql.warehouse.WarehouseMapper;
import cn.iocoder.yudao.module.hs.enums.warehouse.WarehouseTypeEnum;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.WAREHOUSE_NOT_EXISTS;

/**
 * 仓库信息 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createWarehouse(WarehouseSaveReqVO createReqVO) {
        // 插入
        WarehouseDO warehouse = BeanUtils.toBean(createReqVO, WarehouseDO.class);
        warehouse.setAreaName(AreaUtils.format(createReqVO.getAreaId()));
        warehouseMapper.insert(warehouse);
        // 返回
        return warehouse.getId();
    }

    @Override
    public void updateWarehouse(WarehouseSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseExists(updateReqVO.getId());
        // 更新
        WarehouseDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseDO.class);
        updateObj.setAreaName(AreaUtils.format(updateReqVO.getAreaId()));
        warehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouse(Long id) {
        // 校验存在
        validateWarehouseExists(id);
        // 删除
        warehouseMapper.deleteById(id);
    }

    private void validateWarehouseExists(Long id) {
        if (warehouseMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseDO getWarehouse(Long id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        AdminUserRespDTO user = adminUserApi.getUser(getLoginUserId());
        WarehouseDO warehouse = this.getWarehouse(user.getWarehouseId());
        if (WarehouseTypeEnum.TRIPARTITE.getType().equals(warehouse.getWarehouseType())) {
            pageReqVO.setCode(warehouse.getCode());
        }
        return warehouseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseDO> getWarehouseList(WarehousePageReqVO pageReqVO) {
        if (StrUtil.isEmpty(pageReqVO.getWarehouseType())) {
            return warehouseMapper.selectList();
        }
        return warehouseMapper.selectList(WarehouseDO::getWarehouseType, pageReqVO.getWarehouseType());
    }

    @Override
    public WarehouseDO getWarehouse(String code) {
        return warehouseMapper.selectOne(WarehouseDO::getCode, code);
    }

    @Override
    public WarehouseDO getFromWarehouse(String code) {
        LambdaQueryWrapperX<WarehouseDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.likeIfPresent(WarehouseDO::getCode, code);
        return warehouseMapper.selectOne(queryWrapperX);
    }

}